package number_operatation.leetcode.medium;

/**
 * @author bruin_du
 * @description 数值的整数次方
 * @date 2022/6/5 10:51
 **/
public class Offer16_MyPow {
    //快速幂
    public double myPow(double x, int n) {
        if(x == 0) return 0;
        long n1 = n;
        if(n1 < 0) {
            n1 = -n1;
            x = 1 / x;
        }
        double res = 1.0;
        while(n1 > 0) {
            if((n1 & 1) == 1) res *= x;
            x *= x;
            n1 >>= 1;
        }
        return res;
    }
}
